<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:p="http://primefaces.org/ui"
                xmlns:pe="http://primefaces.org/ui/extensions"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core">

<p:dialog id="order_dialog" header="#{orderAdministrationBean.orderDialogTitle}" widgetVar="orderWV" dynamic="true"
          minHeight="300" width="800" closeOnEscape="true" resizable="false" closable="false"
          modal="true">

<h:form id="orders_dialog_form">

    <pe:masterDetail id="master_detail" showAllBreadcrumbItems="true"
                     level="#{orderAdministrationBean.currentLevel}">

        <pe:masterDetailLevel id="order_level_1" level="1" levelLabel="Colectare date ordin">
            <p:outputPanel id="order_level_1_panel" layout="block">
                <p:watermark value="Introduceti numarul ordinului" for="number_id"/>
                <p:watermark value="Introduceti data ordinului" for="ordin_date_id"/>
                <pe:tooltip global="true" myPosition="left center" atPosition="right center"
                            for="@(.ui-state-error)"/>

                <h:panelGrid columns="4" columnClasses="column-1, column-200, column-1, column-200"
                             styleClass="form-elements">
                    <p:outputLabel for="number_id" value="Number:"/>
                    <p:inputText id="number_id" value="#{orderAdministrationBean.selectedOrder.orderNumber}"/>

                    <p:outputLabel for="ordin_date_id" value="Data ordin:"/>
                    <p:calendar id="ordin_date_id" value="#{orderAdministrationBean.selectedOrder.orderDate}"
                                showOn="Button"/>

                    <p:outputLabel for="ordin_type_id" value="Tip:"/>
                    <p:selectOneMenu id="ordin_type_id" value="#{orderAdministrationBean.selectedOrder.orderType}"
                                     converter="#{objectMappingConverter}" required="true"
                                     title="#{component.valid ? '' : 'Tipul ordinului este necesar'}">
                        <f:selectItem itemLabel="Selecteaza..."/>
                        <f:selectItems var="t" itemLabel="#{t.name}" itemValue="#{t}"
                                       value="#{orderAdministrationBean.orderTypes}"/>
                        <p:ajax process="@this" update="order_level_1_panel"
                                listener="#{orderAdministrationBean.resetLevel2}"/>
                    </p:selectOneMenu>

                    <p:outputLabel for="issuer_id" value="Emitent:"/>
                    <p:selectOneMenu id="issuer_id" value="#{orderAdministrationBean.selectedOrder.issuer}"
                                     converter="#{objectMappingConverter}" required="true"
                                     title="#{component.valid ? '' : 'Emitentul ordinului este necesar'}">
                        <f:selectItem itemLabel="Selecteaza..."/>
                        <f:selectItems var="i" itemLabel="#{i.name}" itemValue="#{i}"
                                       value="#{orderAdministrationBean.issuers}"/>
                    </p:selectOneMenu>

                    <p:outputLabel for="folder_id" value="Dosar:"
                                   rendered="#{orderAdministrationBean.checkOrderTypeState('EMPLOYMENT')}"/>
                    <p:selectOneMenu id="folder_id" value="#{orderAdministrationBean.selectedOrder.folder}"
                                     converter="#{objectMappingConverter}" required="true"
                                     rendered="#{orderAdministrationBean.checkOrderTypeState('EMPLOYMENT')}">
                        <f:selectItem itemLabel="Selecteaza..."/>
                        <f:selectItems var="folder"
                                       itemLabel="#{folder.firstName} #{folder.lastName} (#{folder.patronymic})"
                                       itemValue="#{folder}" value="#{orderAdministrationBean.folders}"/>
                    </p:selectOneMenu>

                    <p:outputLabel for="promotion_level_id" value="Grad:"
                                   rendered="#{orderAdministrationBean.checkOrderTypeState('ADVANCEMENT')}"/>
                    <p:selectOneMenu id="promotion_level_id"
                                     value="#{orderAdministrationBean.selectedOrder.advancementRank}"
                                     converter="#{objectMappingConverter}"
                                     rendered="#{orderAdministrationBean.checkOrderTypeState('ADVANCEMENT')}">
                        <f:selectItem itemLabel="Selecteaza..."/>
                        <f:selectItems var="level" itemLabel="#{level.name}" itemValue="#{level}"
                                       value="#{orderAdministrationBean.advancementRanks}"/>
                    </p:selectOneMenu>

                    <p:outputLabel for="certificate_no" value="Numar certificat nastere copil:"
                                   rendered="#{orderAdministrationBean.checkOrderType('PAID_MATERNITY_LEAVE')}"/>
                    <p:outputLabel for="certificate_no" value="Numar certificat deces:"
                                   rendered="#{orderAdministrationBean.checkOrderType('DEATH_CASE')}"/>
                    <p:outputLabel for="certificate_no" value="Numar contract:"
                                   rendered="#{orderAdministrationBean.checkOrderTypeState('RESTORING')}"/>
                    <p:inputText id="certificate_no" value="#{orderAdministrationBean.selectedOrder.docNumber}"
                                 rendered="#{orderAdministrationBean.checkOrderType('PAID_MATERNITY_LEAVE') || orderAdministrationBean.checkOrderType('DEATH_CASE') || orderAdministrationBean.checkOrderTypeState('RESTORING')}"/>

                    <p:outputLabel for="issued_by" value="Eliberat de:"
                                   rendered="#{orderAdministrationBean.checkOrderType('DEATH_CASE')}"/>
                    <p:inputText id="issued_by" value="#{orderAdministrationBean.selectedOrder.issuedBy}"
                                 rendered="#{orderAdministrationBean.checkOrderType('DEATH_CASE')}"/>

                    <p:outputLabel for="doc_date" value="Data emiterii certificat:"
                                   rendered="#{orderAdministrationBean.checkOrderType('PAID_MATERNITY_LEAVE') || orderAdministrationBean.checkOrderType('DEATH_CASE')}"/>
                    <p:calendar id="doc_date" value="#{orderAdministrationBean.selectedOrder.docDate}"
                                rendered="#{orderAdministrationBean.checkOrderType('PAID_MATERNITY_LEAVE') || orderAdministrationBean.checkOrderType('DEATH_CASE')}"
                                required="true" title="#{component.valid ? '' : 'Valoarea este necesara'}"/>

                    <p:outputLabel for="transfer_from" value="Transfer de la:"
                                   rendered="#{orderAdministrationBean.checkOrderType('TRANSFER')}"/>
                    <p:inputText id="transfer_from" value="#{orderAdministrationBean.selectedOrder.transferFrom}"
                                 rendered="#{orderAdministrationBean.checkOrderType('TRANSFER')}" required="true"/>

                    <p:outputLabel for="trial_period" value="Perioada de proba"
                                   rendered="#{orderAdministrationBean.showTrialPeriod()}"/>
                    <pe:inputNumber id="trial_period" value="#{orderAdministrationBean.selectedOrder.trialPeriod}"
                                    symbol=" luni" symbolPosition="s" emptyValue="empty" required="true"
                                    decimalPlaces="0"
                                    rendered="#{orderAdministrationBean.showTrialPeriod()}"/>

                    <p:outputLabel for="education_institution" value="Institutia de invatamant:"
                                   rendered="#{orderAdministrationBean.displayEducationInstitution()}"/>
                    <p:inputText id="education_institution"
                                 value="#{orderAdministrationBean.selectedOrder.educationInstitutionName}"
                                 required="true"
                                 rendered="#{orderAdministrationBean.displayEducationInstitution()}"/>

                    <p:outputLabel for="from_date_id" value="Din data:"
                                   rendered="#{orderAdministrationBean.showRangeDate() || orderAdministrationBean.checkOrderTypeState('DISCHARGED') || orderAdministrationBean.checkOrderType('PERMUTATION')}"/>
                    <p:calendar id="from_date_id" value="#{orderAdministrationBean.selectedOrder.fromDate}"
                                required="true"
                                rendered="#{orderAdministrationBean.showRangeDate() || orderAdministrationBean.checkOrderTypeState('DISCHARGED') || orderAdministrationBean.checkOrderType('PERMUTATION')}"/>

                    <p:outputLabel for="to_date_id" value="Pana la data:"
                                   rendered="#{orderAdministrationBean.showRangeDate()}"/>
                    <p:outputLabel for="to_date_id" value="Sancţiunea expiră:"
                                   rendered="#{orderAdministrationBean.checkOrderTypeState('SANCTIONS')}"/>
                    <p:calendar id="to_date_id" value="#{orderAdministrationBean.selectedOrder.toDate}"
                                rendered="#{orderAdministrationBean.showToDate()}" required="true"
                                title="#{component.valid ? '' : 'Valoarea este necesar'}"/>

                    <p:outputLabel for="reason" value="Motivul:"
                                   rendered="#{orderAdministrationBean.checkOrderTypeState('SUSPENDED')}"/>
                    <p:inputTextarea id="reason" value="#{orderAdministrationBean.selectedOrder.reason}"
                                     rendered="#{orderAdministrationBean.checkOrderTypeState('SUSPENDED')}"/>

                    <p:outputLabel for="location" value="Deplasare la:"
                                   rendered="#{orderAdministrationBean.checkOrderType('DISPLACEMENT')}"/>
                    <p:outputLabel for="location" value="Detasare la:"
                                   rendered="#{orderAdministrationBean.checkOrderType('DETACHMENT')}"/>
                    <p:inputText id="location" value="#{orderAdministrationBean.selectedOrder.location}"
                                 rendered="#{orderAdministrationBean.displayLocation()}"/>

                    <p:outputLabel for="grounds" value="Temei:"
                                   rendered="#{orderAdministrationBean.displayGrounds()}"/>
                    <p:inputTextarea id="grounds" value="#{orderAdministrationBean.selectedOrder.ground}"
                                     required="true" rendered="#{orderAdministrationBean.displayGrounds()}"/>

                    <p:outputPanel layout="block"
                                   rendered="#{orderAdministrationBean.checkOrderTypeState('TRAINING')}">
                        <ui:include src="courses-content.xhtml"/>
                    </p:outputPanel>
                </h:panelGrid>

                <p:outputPanel layout="block" style="border-top: 1px solid #A8A8A8; padding: 5px;">
                    <p:commandButton value="Renunta"
                                     onclick="PF('orderWV').hide(); TreeCustomBehavior.onTransferOff();"
                                     process="@none"
                                     update=":photo-cantainer-form:order_type_groups master_detail"
                                     resetValues="true">
                        <pe:selectDetailLevel level="1"/>
                    </p:commandButton>

                    <p:commandButton value="Urmatorul" process="order_level_1" icon="ui-icon-arrowthick-1-e"
                                     update="master_detail"
                                     oncomplete="repositioningDialog(args &amp;&amp; !args.validationFailed);">
                        <pe:selectDetailLevel/>
                    </p:commandButton>
                </p:outputPanel>
            </p:outputPanel>
        </pe:masterDetailLevel>

        <pe:masterDetailLevel id="order_level_2" level="2" levelLabel="Generare ordin">

            <p:outputPanel layout="block" style="margin: 5px auto; width: 750px;">
                <p:selectOneMenu style="width: 250px; margin-bottom: 5px;" required="true">
                    <f:selectItem itemLabel="Selecteaza sablon ..."/>
                    <f:selectItems var="o" value="#{orderAdministrationBean.selectedOrder.orderType.orderPatterns}"
                                   itemLabel="#{o.name}" itemValue="#{o.id}"/>
                    <p:ajax listener="#{orderAdministrationBean.onOrderPatternSelect}" process="@this"
                            update="ckEditor_id"/>
                </p:selectOneMenu>

                <pe:ckEditor id="ckEditor_id" height="300" width="750"
                             value="#{orderAdministrationBean.selectedOrder.orderText}"
                             contentsCss="../../../../../resources/css/order-template.css"
                             customConfig="#{request.contextPath}/resources/js/ckeditor_config.js"/>
            </p:outputPanel>

            <p:outputPanel layout="block" style="border-top: 1px solid #A8A8A8; padding: 5px;">
                <p:commandButton value="Inapoi" icon="ui-icon-arrowthick-1-w" process="@none"
                                 update="master_detail">
                    <pe:selectDetailLevel step="-1"/>
                </p:commandButton>
                <p:commandButton value="Finisare" actionListener="#{orderAdministrationBean.finishOrderCreation}"
                                 process="order_level_2"
                                 update=":folder-info-form :tree_view_form :photo-cantainer-form:order_type_groups master_detail"
                                 oncomplete="if (args &amp;&amp; !args.validationFailed) orderWV.hide(); TreeCustomBehavior.onTransferOff();">
                    <pe:selectDetailLevel step="-1"/>
                </p:commandButton>
            </p:outputPanel>
        </pe:masterDetailLevel>

    </pe:masterDetail>

</h:form>
<h:outputScript>
    function repositioningDialog(valid) {
        if (valid) {
            $('#order_dialog').css("top", "0");
        }
    }
</h:outputScript>
</p:dialog>
</ui:composition>
